﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Web.UI
{
    public class ZPageList<TModel> : ZMPage where TModel : BaseModel, new()
    {
        protected virtual int PageSize
        {
            get
            {
                return 20;
            }
        }
        protected virtual string Columns
        {
            get { return "*"; }
        }
        protected int? RecordCount
        {
            get
            {
                if (ViewState["RecordCount"] == null)
                {
                    ViewState["RecordCount"] = -1;
                }
                return (int?)ViewState["RecordCount"];
            }
            set
            {
                ViewState["RecordCount"] = value;
            }
        }
        protected virtual string Sort
        {
            get
            {
                string sort = "";
                if (ViewState["Sort"] == null)
                {
                    if (!string.IsNullOrEmpty(DefaultSortField))
                        sort = DefaultSortField + " " + DefaultSortList;
                    ViewState["Sort"] = sort;
                }
                return (string)ViewState["Sort"];
            }
            set
            {
                ViewState["Sort"] = value;
            }
        }
        protected virtual string LastSort
        {
            get
            {
                string sort = "";
                if (ViewState["LastSort"] != null)
                {
                    sort = (string)ViewState["LastSort"];
                }
                return sort;
            }
            set
            {
                ViewState["LastSort"] = value;
            }
        }
        protected virtual string TempSort
        {
            get
            {
                string sort = "";
                if (ViewState["TempSort"] != null)
                {
                    sort = (string)ViewState["TempSort"];
                }
                return sort;
            }
            set
            {
                ViewState["TempSort"] = value;
            }
        }
        protected bool? IsSortChanging { get; set; }
        protected virtual string GetSort()
        {
            string sort = Sort;

            if (string.IsNullOrEmpty(LastSort))
            {
                LastSort = sort;
            }
            if (string.IsNullOrEmpty(TempSort))
            {
                TempSort = sort;
            }

            string lastSort = LastSort;
            string tempSort = TempSort;

            string lastSortField = lastSort.Split(' ')[0].ToLower();
            string sortField = sort.Split(' ')[0].ToLower();
            string tempSortField = tempSort.Split(' ')[0].ToLower();

            if (IsSortChanging.HasValue && IsSortChanging.Value)
            {
                if (lastSortField != sortField && tempSortField != sortField)
                {
                    LastSort = TempSort;
                    TempSort = Sort;
                    lastSort = LastSort;
                    tempSort = TempSort;

                    lastSortField = lastSort.Split(' ')[0].ToLower();
                    tempSortField = tempSort.Split(' ')[0].ToLower();
                }
                else
                {
                    if (sortField == lastSortField)
                    {
                        LastSort = TempSort;
                        lastSort = LastSort;
                        sortField = sort.Split(' ')[0].ToLower();
                    }
                }
            }
            if (lastSortField != sortField)
            {
                sort = sort + "," + lastSort;
            }

            return sort;

        }
        protected virtual string DefaultSortList
        {
            get
            {
                return "DESC";
            }
        }
        protected virtual string DefaultSortField
        {
            get
            {
                TModel t = new TModel();
                return t.GetPrimaryKey();
            }
        }
        protected virtual string DefaultFilter
        {
            get { return "1=1"; }
        }
        protected virtual string Filter
        {
            get
            {
                if (ViewState["Filter"] == null)
                {
                    ViewState["Filter"] = DefaultFilter;
                }
                return (string)ViewState["Filter"];
            }
            set
            {
                ViewState["Filter"] = DefaultFilter + " AND " + value;
            }
        }
        protected virtual List<DBParameter> DBParameters
        {
            get
            {
                if (ViewState["DBParameters"] == null)
                {
                    ViewState["DBParameters"] = new List<DBParameter>();
                }
                return (List<DBParameter>)ViewState["DBParameters"];
            }
            set
            {
                ViewState["DBParameters"] = value;
            }
        }
        protected override void RegControls()
        {
            base.RegControls();
            RegListControls();
        }
        protected virtual void RegListControls()
        {

        }
        protected override void RegControlsEvent()
        {
            base.RegControlsEvent();
            RegListControlsEvent();
        }
        protected virtual void RegListControlsEvent()
        {

        }
        protected override void OnLoad()
        {
            base.OnLoad();
            OnGridDataBinding();
            OnListPageLoad();
            OnGridDataBinded();
        }
        protected virtual void OnListPageLoad()
        {
            if (!IsPostBack)
            {
                OnGridDataBind();
            }
        }
        protected virtual void OnGridChanged()
        {
            OnGridDataBind();
            OnGridDataBinded();
        }
        protected virtual void OnGridDataBind()
        {

        }
        protected virtual void OnGridDataBinding()
        {

        }
        protected virtual void OnGridDataBinded()
        {

        }
    }
}
